package com.xilihui.xlh.business.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.ShopDetailEntity;
import com.xilihui.xlh.business.util.AMapUtil;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ToastUtil;

import java.net.URISyntaxException;

import retrofit2.http.Url;

/**
 * Created by Administrator on 2018/7/17.
 */

public class MapDialog implements View.OnClickListener {

    private Dialog dialog;
    private Activity activity;
//    private ShopDetailEntity.AgentBean agentBean;
    private String lat = "";
    private String lng = "";

    public MapDialog(Activity activity,String lat,String lng) {
        this.activity = activity;
        this.lat = lat;
        this.lng = lng;
        
        dialog = new Dialog(activity, R.style.Dialog);
        dialog.setContentView(R.layout.dialog_map);
        // Dialog在底部
        Window window = dialog.getWindow();
        if (window != null)
            window.setGravity(Gravity.BOTTOM);
        // Dialog与屏幕同宽
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = activity.getResources().getDisplayMetrics().widthPixels; // 设置宽度
        dialog.getWindow().setAttributes(lp);

        dialog.findViewById(R.id.tv_baidu).setOnClickListener(this);
        dialog.findViewById(R.id.tv_gaode).setOnClickListener(this);
        dialog.findViewById(R.id.tv_tengxun).setOnClickListener(this);
        dialog.findViewById(R.id.tv_cancel).setOnClickListener(this);
        dialog.setCanceledOnTouchOutside(true);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_baidu:
                if (AMapUtil.isInstallByRead("com.baidu.BaiduMap")) {
                    try {
                        Intent intent = Intent.getIntent("intent://map/direction?" +
                                "destination=latlng:" + lat + "," + lng + "|name:我的目的地" +    //终点
                                "&mode=driving&" +     //导航路线方式
                                "&src=appname#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                        activity.startActivity(intent); //启动调用
                    } catch (URISyntaxException e) {
                        LogUtil.e("intent", e.getMessage());
                    }
                } else {//未安装
                    //market为路径，id为包名
                    //显示手机上所有的market商店
                    ToastUtil.toastShortNegative("您尚未安装百度地图");
                    Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    if (intent.resolveActivity(activity.getPackageManager()) != null) {
                        activity.startActivity(intent);
                    }
                }
                break;
            case R.id.tv_gaode:
                if (AMapUtil.isInstallByRead("com.autonavi.minimap")) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    //将功能Scheme以URI的方式传入data
                    Uri uri = Uri.parse("androidamap://navi?sourceApplication=appname&poiname=fangheng&lat=" + lat + "&lon=" + lng + "&dev=1&style=2");
                    intent.setData(uri);

                    //启动该页面即可
                    activity.startActivity(intent);
                } else {
                    ToastUtil.toastShortNegative("您尚未安装高德地图");
                    Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    if (intent.resolveActivity(activity.getPackageManager()) != null) {
                        activity.startActivity(intent);
                    }
                }
                break;
            case R.id.tv_tengxun:
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);

                //将功能Scheme以URI的方式传入data
                Uri uri = Uri.parse("qqmap://map/routeplan?type=drive&to=我的目的地&tocoord=" + lat + "," + lng);
                intent.setData(uri);
                if (intent.resolveActivity(activity.getPackageManager()) != null) {
                    //启动该页面即可
                    activity.startActivity(intent);
                } else {
                    ToastUtil.toastShortNegative("您尚未安装腾讯地图");
                    Uri uri1 = Uri.parse("market://details?id=com.tencent.map");
                    Intent intent1 = new Intent(Intent.ACTION_VIEW,uri1);
                    if(intent1.resolveActivity(activity.getPackageManager())!=null){
                        activity.startActivity(intent1);
                    }
                }
                break;
            case R.id.tv_cancle:
                
                break;
        }
        dialog.hide();
    }

    public void show() {
        dialog.show();
    }
}
